package view;

import java.util.List;
import javax.swing.table.DefaultTableModel;

import model.MeetingInfo;

/**
 * Class MeetingModel
 *
 */
public class MeetingModel extends DefaultTableModel
{
    private static final long serialVersionUID = 543095181677302605L;

    private String[] columnNames;
    private List<MeetingInfo> meetings;
    
    /**
     * Constructor for class MeetingModel  
     * @param List<MeetingInfo> allMeetingInfo the list of all meeting info
     */
    public MeetingModel(List<MeetingInfo> allMeetingInfo)
    {
       this.meetings = allMeetingInfo;
       this.columnNames = new String[]{"From", "Subject", "Start Date&Time", "End Date&Time"};    
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#setValueAt(java.lang.Object, int, int)
     */
    public void setValueAt(Object value, int row, int col)
    {         
        fireTableCellUpdated(row, col);
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int)
     */
    public boolean isCellEditable(int row, int column)
    {
        return false;
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    public Class<?> getColumnClass(int columnIndex)
    {
        return getValueAt(0, columnIndex).getClass();
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#getColumnName(int)
     */
    public String getColumnName(int columnIndex)
    {
        return this.columnNames[columnIndex];
    }
/*
 * (non-Javadoc)
 * @see javax.swing.table.DefaultTableModel#getRowCount()
 */
    public int getRowCount()
    {
        if(this.meetings == null)
        {
            return 0;
        }
        return this.meetings.size();
    }

    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#getColumnCount()
     */
    public int getColumnCount()
    {
        return columnNames.length;
    }
    
    /*
     * (non-Javadoc)
     * @see javax.swing.table.DefaultTableModel#getValueAt(int, int)
     */
    public Object getValueAt(int rowIndex, int columnIndex)
    {
        MeetingInfo meeting =  meetings.get(rowIndex);
        

            switch (columnIndex) 
            {
                case 0:
                    return meeting.sender;
                case 1:
                   return meeting.originalSubject;
                case 2:
                   return meeting.originalStartTime;
                case 3:
                    return meeting.originalEndiTime;
                default:
                   return new Object();
            }
    
    }

    /**
     * get meeting associated with the give row
     * @param row the given row
     * @return return the MeetingInfo object in the given row
     */
    public MeetingInfo getEmailAssociatedToRow(int row)
    {
        return this.meetings.get(row);
    }
}
